package cn.itrip.trade.service;

import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripHotelTempStore;
import cn.itrip.beans.pojo.ItripTradeEnds;
import cn.itrip.dao.hotelorder.ItripHotelOrderMapper;
import cn.itrip.dao.hoteltempstore.ItripHotelTempStoreMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;

    @Override
    public ItripHotelOrder loadItripHotelOrder(String orderNo) throws Exception{
        Map<String, Object> param = new HashMap();
        param.put("orderNo",orderNo);
        List<ItripHotelOrder> list = itripHotelOrderMapper.getItripHotelOrderListByMap(param);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public void paySuccess(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder order = this.loadItripHotelOrder(orderNo);
        order.setOrderStatus(2);
        order.setPayType(payType);
        order.setTradeNo(tradeNo);
        this.itripHotelOrderMapper.updateItripHotelOrder(order);
        //减库存，由业务提供API，在这里调用
        ItripTradeEnds itripTradeEnds = new ItripTradeEnds();
        Map<String, Object> roomStoreMap = new HashMap<String, Object>();
        roomStoreMap.put("startTime", order.getCheckInDate());
        roomStoreMap.put("endTime", order.getCheckOutDate());
        roomStoreMap.put("count", order.getCount());
        roomStoreMap.put("roomId", order.getRoomId());
        itripHotelTempStoreMapper.updateRoomStore(roomStoreMap);
    }

    @Override
    public void payFailed(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder order = this.loadItripHotelOrder(orderNo);
        order.setOrderStatus(1);
        order.setPayType(payType);
        order.setTradeNo(tradeNo);
        this.itripHotelOrderMapper.updateItripHotelOrder(order);
    }
}
